﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

namespace HongStone.Common.Filter
{
    public class CustomerExceptionfilter : IExceptionFilter
    {

        private readonly ILogger<CustomerExceptionfilter> _logger;
        public CustomerExceptionfilter( ILogger<CustomerExceptionfilter> logger)
        {
            _logger = logger;
            _logger.LogDebug(1, "NLog injected into HomeController");
        }

        public void OnException(ExceptionContext context)
        {
            //当前异常未处理
            if(context.ExceptionHandled == false)
            {
                //处理异常
                Exception ex = context.Exception;
                if(ex!=null)
                {
                    //获取异常信息
                    string msg = ex.Message;
                    //日志
                    _logger.LogInformation(msg);

                }

            }

            //异常已经处理成功
            context.ExceptionHandled = true;
        }
    }
}
